package com.fastgeek.platform.db.datasource;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

/**
 * @Title  IXmlParser.java
 * @author zhangliang 
 * @Description: 
 * @version $Rev$
 * @LastModify: $Id$ 
 */
public abstract class IXmlParser {
	public Map load(String configFileName) {
		Map<String, Object> mps = new LinkedHashMap<String, Object>();
		try {
			if (configFileName == null || configFileName.trim().length() == 0)
				return mps;

			Document doc = buildDocument(configFileName);
			if (doc == null)
				return mps;

			Element root = doc.getRootElement();
			parse(root, mps);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mps;
	}

	protected Document buildDocument(String configFileName) {
		Document doc = null;
		try {
			InputStream xmlStream = getInputStream(configFileName);

			SAXBuilder builder = new SAXBuilder();
			doc = builder.build(xmlStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}

	protected InputStream getInputStream(String configFileName)
			throws Exception {
		String path = "";
		path = IXmlParser.class.getResource("/").toURI().getPath();
		path = new File(path).getParentFile().getParentFile()
				.getCanonicalPath();
		return new FileInputStream(new File(IXmlParser.class.getResource("/")
				.toURI().getPath()
				+ "/META-INF/" + configFileName));
	}

	public abstract void parse(Element root, Map results) throws Exception;
}
